package cn.wx.system.service.impl;

import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.system.domain.Role;
import cn.wx.system.mapper.RoleMapper;
import cn.wx.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
// 父类有事务，该注解有继承性
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void save(Role role) {
        // 三张表   role(返回自增的id)
        roleMapper.save(role);

        if(role.getPermissions().size()>0){
            // 添加t_role_permission表
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            //添加到t_role_menu表
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除 ， 先删除关联表，再删除自己

        roleMapper.removeEmployeeRole(id);
        roleMapper.removeRoleMenu(id);
        roleMapper.removeRolePermission(id);

        // 删除自己
        roleMapper.remove(id);

    }


    @Override
    public void update(Role role) {
        //先把自己修改了
        roleMapper.update(role);

        //删除t_role_menu
        roleMapper.removeRoleMenu(role.getId());
        //删除t_role_permission
        roleMapper.removeRolePermission(role.getId());

        //再次修改添加，重新添加进去
        if(role.getPermissions().size()>0){
            // 添加t_role_permission表
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            //添加到t_role_menu表
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

    }
}
